/**
 * @filename:ProcureController Mar 5, 2024
 * @project Wonder Vue Admin Api  1.1.0
 * Copyright(c) 2020 Wonder Co. Ltd. 
 * All right reserved. 
 */
package com.wonder.app.controller;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Date;
import java.util.Map;

import com.vtuzx.core.bean.dto.VtuzxMap;
import com.wonder.admin.domain.SysAdmin;
import com.wonder.admin.service.SysAdminService;
import com.wonder.app.domain.Procure;
import com.wonder.app.domain.SupplimentDetail;
import com.wonder.app.service.ProcureDetailService;
import com.wonder.app.service.ProcureService;
import com.wonder.app.request.ProcureRequest;
import com.wonder.framework.authorization.CheckAdminToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.wonder.common.common.AjaxMap;
import com.wonder.common.common.AjaxResult;
import com.wonder.common.common.CommonPage;
import com.wonder.common.utils.MessageUtils;
import com.wonder.common.utils.StringUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;

import javax.servlet.http.HttpServletRequest;

/**
 * 
 * <p>说明： 采购入库API接口层</P>
 * @version: 1.1.0
 * @author: Wonder
 * @time    Mar 5, 2024
 *
 */
@Api(tags = "采购入库")
@RestController
@RequestMapping("/app/Procure")
public class ProcureController {
	
	@Autowired
	private ProcureService procureService;

	@Autowired
	private CheckAdminToken checkAdminToken;

	@Autowired
	private SysAdminService adminService;

	@Autowired
	private ProcureDetailService procureDetailService;
	
	@ApiOperation(value = "采购入库列表")
	@RequestMapping(value = "/index", method = RequestMethod.POST)
	public Map<String,Object> index(@RequestBody ProcureRequest procureRequest) {
		Page<Procure> page = new Page<Procure>(procureRequest.getPage(), procureRequest.getLimit());
		IPage<Procure> list = procureService.selectList(procureRequest, page);
		
		Map<String,Object> retMap=new HashMap<>();
		retMap.put("data",CommonPage.restPage(list));
		
  
		return AjaxMap.success(retMap);
	}
	
	/**
	 * @explain 修改排序开关
	 * @param   entity
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/updateExt")
	@ApiOperation(value = "修改排序开关", notes = "作者：Wonder")
	public AjaxResult<Procure> updateExt(@RequestBody Procure entity){
		if (null!=entity) {
			boolean rsg = procureService.updateById(entity);
			if (rsg) {
				  return AjaxResult.success("system.updateSuccess");
			  }else {
				  return AjaxResult.failed("system.updateFailed");
			  }
		}else {
			return AjaxResult.failed("system.parameterError");
		}
	}
	
	/**
	 * @explain 添加
	 * @param   entity
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/add")
	@ApiOperation(value = "添加", notes = "作者：Wonder")
	public AjaxResult<Procure> add(@RequestBody Procure entity){

		if (null!=entity) {
		    
		    String validate=procureService.validate(entity,"new");
		    if(StringUtils.hasText(validate))
		         return AjaxResult.failed(validate);    
			boolean rsg = procureService.save(entity);
			if (rsg) {
				  return AjaxResult.success("system.insertSuccess");
			  }else {
				  return AjaxResult.failed("system.insertFailed");
			  }
		}else {
			return AjaxResult.failed("system.parameterError");
		}
	}
	
	/**
	 * @explain 查询对象  <swagger GET请求>
	 * @param   map 对象参数：id
	 * @return  JsonResult
	 * @author  Wonder
	 */
	@PostMapping("/getUpdateInfo")
	@ApiOperation(value = "获取采购入库对象", notes = "作者：Wonder")
	public AjaxResult<Procure> getUpdateInfo(@RequestBody Map<String,Object> map){
	    if(map==null) return AjaxResult.failed("system.parameterError");
		Long id=Long.parseLong(map.get("id").toString());		
		 Procure procure=procureService.getById(id);
		
		if (null!=procure ) {
			 return AjaxResult.success(procure);
		}else {
			 return AjaxResult.failed("system.parameterError");
		}
		
	}
	
	/**
	 * @explain 修改
	 * @param   entity
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/update")
	@ApiOperation(value = "修改", notes = "作者：Wonder")
	public AjaxResult<Procure> update(@RequestBody Procure entity){
		if (null!=entity) {
		    String validate=procureService.validate(entity,"update");
		    if(StringUtils.hasText(validate))
		         return AjaxResult.failed(validate);
			boolean rsg = procureService.updateById(entity);
			if (rsg) {
				  return AjaxResult.success("system.updateSuccess");
			  }else {
				  return AjaxResult.failed("system.updateFailed");
			  }
		}else {
			return AjaxResult.failed("system.parameterError");
		}
	}
	
	/**
	 * @explain 删除
	 * @param   map 对象参数：id
	 * @return  JsonResult
	 * @author  Wonder
	 */
	@PostMapping("/delete")
	@ApiOperation(value = "删除", notes = "作者：Wonder")
	public AjaxResult<Procure> delete(@RequestBody Map<String,Object> map){
		if(map==null)return AjaxResult.failed("system.parameterError");
		String ids=map.get("id").toString();
		ids=StringUtils.remove(ids, '[');
		ids=StringUtils.remove(ids, ']');
		String[] deleteIds=StringUtils.split(ids, ',');
		List<Long> idList=new ArrayList<>(); 
		for(int i=0;i<deleteIds.length;i++)
		{
			idList.add( Long.parseLong(deleteIds[i].trim()));//整数数组
		}

	    boolean rsg = procureService.removeBatchByIds(idList);
	    if (rsg) {
		    return AjaxResult.success("system.deleteSuccess");
	    }else {
		   return AjaxResult.failed("system.deleteFailed");
	    }
		
	}
	
	/**
	 * @explain 查看对象明细  
	 * @param   map 对象参数：id
	 * @return  JsonResult
	 * @author  Wonder
	 */
	@PostMapping("/detail")
	@ApiOperation(value = "获取采购入库对象明细", notes = "作者：Wonder")
	public AjaxResult<VtuzxMap> detail(@RequestBody Map<String,Object> map){
	    if(map==null) return AjaxResult.failed("system.parameterError");
		String billNo = map.get("billNo").toString();
		VtuzxMap data = procureService.getDetail(billNo);
		if (null!=data ) {
			 return AjaxResult.success(data);
		}else {
			 return AjaxResult.failed("system.parameterError");
		}
		
	}

	/**
	 * 反审采购入库单
	 * @param map
	 * @return
	 */
	@PostMapping("/unAudit")
	@ApiOperation(value = "反审核其他入库单", notes = "作者：sunyf")
	public AjaxResult<Boolean> unAudit(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception{
		String token = checkAdminToken.getTokenFormRequest(request);
		SysAdmin user = adminService.getAdminByToken(token);
		String userName = user.getName();
		Long user_id = user.getUser_id();
		if(map==null) return AjaxResult.failed("system.parameterError");
		Long id=Long.parseLong(map.get("id").toString());
		boolean flag = procureService.unAuditProcure(user_id,userName,id);
		return AjaxResult.success(flag);
	}

	/**
	 * 审核采购入库单
	 * @return
	 */
	@PostMapping("/audit")
	@ApiOperation(value = "审核采购入库单", notes = "作者：sunyf")
	public AjaxResult<Boolean> audit(HttpServletRequest request,@RequestBody Map<String,Object> map)throws Exception{
		String token = checkAdminToken.getTokenFormRequest(request);
		SysAdmin user = adminService.getAdminByToken(token);
		String userName = user.getName();
		Long user_id = user.getUser_id();
		if(map==null) return AjaxResult.failed("system.parameterError");
		String billNo = map.get("billNo").toString();
		boolean flag = procureService.auditProcure(user_id,userName,billNo);
		return AjaxResult.success(flag);
	}

	/**
	 * 保存采购入库单
	 * @param request
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/save_procure")
	@ApiOperation(value = "保存采购入库单", notes = "作者：Wonder")
	public AjaxResult<Boolean> saveProcure(HttpServletRequest request,@RequestBody Map<String,Object> map) throws Exception{
		String token = checkAdminToken.getTokenFormRequest(request);
		SysAdmin user = adminService.getAdminByToken(token);
		String userName = user.getName();
		Long user_id = user.getUser_id();
		if(map==null) return AjaxResult.failed("system.parameterError");
		List<Map<String,Object>> dataList = (List<Map<String,Object>>)map.get("dataList");
		Map<String,Object> header = (Map<String, Object>) map.get("header");
		boolean isSuccess = procureService.saveProcure(userName,header,dataList);
		return AjaxResult.success(isSuccess);
	}

	/**
	 * @explain 删除分录
	 * @param   map 对象参数：id
	 * @return  JsonResult
	 * @author  Wonder
	 */
	@PostMapping("/delete_detail")
	@ApiOperation(value = "删除", notes = "作者：Wonder")
	public AjaxResult<SupplimentDetail> deleteDetail(@RequestBody Map<String,Object> map){
		if(map==null)return AjaxResult.failed("system.parameterError");
		Long id = Long.parseLong(map.get("id").toString());
		boolean rsg = procureDetailService.removeById(id);

		if (rsg) {
			return AjaxResult.success("system.deleteSuccess");
		}else {
			return AjaxResult.failed("system.deleteFailed");
		}
	}
}